package info.sixcorners.thesixcornersapp.client;

import info.sixcorners.thesixcornersapp.client.login.LoginPanel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.i18n.client.LocalizableResource.DefaultLocale;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Thesixcornersapp implements EntryPoint {
	@DefaultLocale()
	public static interface TsaMessages extends Messages {
		@DefaultMessage("Welcome to sixcorners.info!")
		String tsaWelcome();
	}
	private static TsaMessages messages = GWT.create(TsaMessages.class);
	
	/**
	 * Everything goes inside this main DockPanel.
	 */
	final DockPanel mainPanel = new DockPanel();
	{
		mainPanel.getElement().setId("mainPanel");
		mainPanel.setSize("100%", "100%");
		RootPanel.get().add(mainPanel);
	}
	
	/**
	 * Top is where login and links will be. As opposed to site name and logo.
	 */
	final HorizontalPanel topPanel = new HorizontalPanel();
	final LoginPanel loginPanel = new LoginPanel(new HorizontalPanel());
	{
		topPanel.getElement().setId("topPanel");
		topPanel.setWidth("100%");
		topPanel.add(new HTMLPanel("<a href=\"http://www.sixcorners.info/\" target=\"_blank\">www.sixcorners.info</a>"));
		topPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		topPanel.add(loginPanel);
		mainPanel.setHorizontalAlignment(DockPanel.ALIGN_RIGHT);
		mainPanel.add(topPanel, DockPanel.NORTH);
	}
	
	final HorizontalPanel northPanel = new HorizontalPanel();
	{
		northPanel.getElement().setId("northPanel");
		northPanel.setWidth("100%");
		northPanel.add(new Label(messages.tsaWelcome()));
		final GreetingPanel greetingPanel = new GreetingPanel();
		northPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		northPanel.add(greetingPanel);
		mainPanel.add(northPanel, DockPanel.NORTH);
	}
	
	final ViewPort centerPanel = new ViewPort();
	{
		centerPanel.getElement().setId("northPanel");
		mainPanel.add(centerPanel, DockPanel.CENTER);
	}
	
	/**
	 * This is the entry point method.
	 * This is executed after the instance initializers.
	 */
	@Override
	public void onModuleLoad() {
		// I guess this only helps after onModuleLoad() returns.
		GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void onUncaughtException(Throwable e) {
				Window.alert("UncaughtException");
			}
		});
		
		{ // OPTIONAL: include this if you want history support
			final Frame historyFrame = new Frame();
			historyFrame.getElement().setId("__gwt_historyFrame");
			historyFrame.getElement().setAttribute("tabIndex", "-1");
			historyFrame.getElement().setAttribute("style", "position:absolute;width:0;height:0;border:0;");
			RootPanel.get().add(historyFrame, 0, 0);
		}
		
		{ //TODO: check if logged in and set the user's style
		}
	}
}
